﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GQ.Controls.Tools;
using GQ.Utils.TreeNode;
using GQ.BLL.QuanLyKho;
using GQ.Utils;
using DevExpress.XtraEditors.Controls;
using GQ.WinForms;

namespace GQ.QuanLyKho
{
    public partial class frmListNhanVien : frmBase
    {
        public frmListNhanVien()
        {
            InitializeComponent();
            TreeControls.SetReadOnly(treeList1);
            GridUtils.SetStyle(gridView1);
        }

        private void frmListNhanVien_Load(object sender, EventArgs e)
        {
            IList<PhongBan> phongs = PhongBan.LoadAllBy("CompanyID={0} and PhongBanChaID={1} Sortby Stt", Config.CompanyID, Guid.Empty);
            NodeCollection nodes = new NodeCollection();

            foreach (PhongBan item in phongs)
            {
                nodes.Add(item);
            }
            TreeControls.LoadDataTree(treeList1, nodes);
            LoadComboPhongBan(nodes, "");
            repositoryItemImageComboBox1.Items.Add(new ImageComboBoxItem("Nam", true));
            repositoryItemImageComboBox1.Items.Add(new ImageComboBoxItem("Nữ", false));
            if (treeList1.FocusedNode != null)
            {
                PhongBan phongban = (PhongBan)treeList1.FocusedNode.Tag;
                gridControl1.DataSource = NhanVien.LoadByPhongBan(phongban.ID);
            }


        }
        private void LoadComboPhongBan(NodeCollection nodes, string index)
        {
            foreach (INode item in nodes)
            {
                rpPhongBan.Items.Add(new ImageComboBoxItem(index + item.Text, item.ID));
                if (item.Children.Count > 0)
                    LoadComboPhongBan(item.Children, index + '-');
            }
        }
        private void treeList1_AfterFocusNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e)
        {
            PhongBan phongban = (PhongBan)e.Node.Tag;
            gridControl1.DataSource = NhanVien.LoadByPhongBan(phongban.ID);
        }

        private void menuButton1_AddNewClick()
        {
            try
            {
                frmNhanVien frm = new frmNhanVien();
                PhongBan phongban = (PhongBan)treeList1.FocusedNode.Tag;
                frm.NhanVien = new NhanVien();
                if (phongban != null)
                    frm.NhanVien.PhongBanID = phongban.ID;

                if (frm.ShowDialog(this) == DialogResult.OK)
                {
                    IList<NhanVien> nhanViens = (IList<NhanVien>)gridControl1.DataSource;
                    nhanViens.Add(frm.NhanVien);
                    Helpers.Project.SetStatus("Lưu thành công", StatusInfo.Complete);
                }
            }
            catch (Exception ex)
            {
                Helpers.Log(ex);
                Helpers.Project.SetStatus("Lưu không thành công", StatusInfo.Error);

            }
            menuButton1.Action = GQ.Controls.ActionState.View;
        }

        private void menuButton1_EditClick()
        {
            try
            {
                NhanVien nhanvien = (NhanVien)gridView1.GetFocusedRow();
                if (nhanvien == null)
                {
                    Helpers.Project.SetStatus("Chưa có nhân viên được chọn", StatusInfo.Info);
                    menuButton1.Action = GQ.Controls.ActionState.View;
                    return;
                }
                frmNhanVien frm = new frmNhanVien();
                frm.NhanVien = nhanvien;
                if (frm.ShowDialog(this) == DialogResult.OK)
                {
                    Helpers.Project.SetStatus("Lưu thành công", StatusInfo.Complete);
                }
            }
            catch (Exception ex)
            {
                Helpers.Log(ex);
                Helpers.Project.SetStatus("Lưu không thành công", StatusInfo.Error);
            }
            menuButton1.Action = GQ.Controls.ActionState.View;
        }

        private void menuButton1_DeleteClick()
        {
            try
            {
                NhanVien nhanvien = (NhanVien)gridView1.GetFocusedRow();
                if (nhanvien == null)
                {
                    Helpers.Project.SetStatus("Chưa có nhân viên được chọn", StatusInfo.Info);
                    return;
                }
                if (Box.Show("Bạn có muốn xóa nhân viên này không?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
                    return;
                nhanvien.Delete();
                IList<NhanVien> nhanViens = (IList<NhanVien>)gridControl1.DataSource;
                nhanViens.Remove(nhanvien);
                Helpers.Project.SetStatus("Xóa thành công", StatusInfo.Complete);
            }
            catch (Exception ex)
            {
                Helpers.Log(ex);
                Helpers.Project.SetStatus("Xóa không thành công", StatusInfo.Error);

            }
        }

        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {
            menuButton1_EditClick();
        }
    }
}
